<?php

namespace App\Api\Filters;


class BooksFilters extends Filters
{

    protected $filters = [
        'popular',
        'updateTime',
        'collection',
        'flowers',
        'words',
        'wordCount',
        'status',
        'updateBetween',
        'gender',
        'type'
    ];

    public static $wordTypes = [
        0 => 0, // 不限
        1 => [0,300000], // 0 - 30 万
        2 => [300000,500000], // 30万-50万
        3 => [500000,1000000], // 50万-100万
        4 => [1000000,2000000], // 100万-200万
        5 => 2000000 // 200万以上
    ];

    public function type($type){

        return $this->builder->where('type',$type);

    }

    public function gender($gender){

        if (!in_array($gender,[0,1])){
            abort(404);
        }

        return $this->builder->where('gender',$gender);

    }

    public function popular($type){

        $this->builder->getQuery()->orders = [];

        if ($type == 0){

            return $this->builder->orderBy('read_counts','asc');
        }

        return $this->builder->orderBy('read_counts','desc');

    }

    public function updateTime(){

        $this->builder->getQuery()->orders = [];


        return $this->builder->orderBy('last_modify_time','desc');
    }

    public function collection($type){

        $this->builder->getQuery()->orders = [];

        if ($type == 0){

            return $this->builder->orderBy('collection_counts','asc');
        }
        return $this->builder->orderBy('collection_counts','desc');

    }

    public function flowers(){

        $this->builder->getQuery()->orders = [];

        return $this->builder->withCount(['rewarded' => function ($query) {
            $query->where('gift_id',1);
        }])->orderBy('rewarded_count','desc');
    }

    public function words(){

        $this->builder->getQuery()->orders = [];

        return $this->builder->orderBy('words','desc');
    }

    public function wordCount($type){

        $wordTypes = static::$wordTypes;

        if (! array_key_exists($type,$wordTypes)){
            return $this->builder;
        }

        $wordType = $wordTypes[$type];

        if (is_array($wordType)){

            return $this->builder->whereBetween('words',$wordType);
        }

        return $this->builder->where('words','>=',$wordType);
    }



    // 1 => 已完结,2=>连载中 不传=> 全部
    public function status($status){

        return $this->builder->where('status',$status);
    }

    public function updateBetween($type){

        $types = [
            1 => 3,         // 三日内
            2 => 7,         // 七日内
            3 => 15,        // 半月内
            4 => 30         // 一月内
        ];

        if (!array_key_exists($type,$types)){
            return $this->builder;
        }

        $updateType = $types[$type];

        $lessDay = $this->lessDay($updateType);

        return $this->builder->where('last_modify_time','>',$lessDay);

    }

    private function lessDay($less){
        return mktime(0, 0 , 0,date("m"),date("d") - $less,date("Y"));
    }
}